package com.zgjkhis.modules.system.service.mapper;

import com.zgjkhis.common.mapper.CoreMapper;
import com.zgjkhis.modules.system.domain.DrugCirculation;
import com.zgjkhis.modules.system.req.DrugCirculationReq;
import com.zgjkhis.modules.system.req.DrugCirculationToReq;
import com.zgjkhis.modules.system.resp.DrugCirculationDetailRes;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author yzq
 * @Description 药品流转
 * @Date 2021/6/23 9:48
 * @Param
 * @return
 **/
@Repository
public interface DrugCirculationMapper extends CoreMapper<DrugCirculation> {

    /**
     * 分页查询流转药品数据
     * @param drugCirculationReq
     * @return
     */
    @Select("<script>select t.* from his_drug_circulation t\n" +
            "left join his_drug dr on t.from_drug_id=dr.id" +
            " where 1=1 and t.status!=-1 and t.from_store_id =#{drugCirculation.fromStoreId}" +
            "<if test = \"drugCirculation.status != null\">\n" +
            "\t and t.status = #{drugCirculation.status}\n" +
            "</if>\n" +
            "<if test=\"drugCirculation.drugName!=null and drugCirculation.drugName!=''\">\n" +
            "\t and (dr.drug_name LIKE CONCAT('%',#{drugCirculation.drugName},'%') or dr.pinyin LIKE CONCAT('%',#{drugCirculation.drugName},'%'))\n" +
            "</if>\n" +
            "<if test=\"drugCirculation.toStoreId!=null and drugCirculation.toStoreId!=''\">\n" +
            "\t and t.to_store_id=#{drugCirculation.toStoreId}\n" +
            "</if>\n" +
            "</script>")
    List<DrugCirculation> queryAll(@Param("drugCirculation") DrugCirculationReq drugCirculationReq);



    @Select("<script>select c.from_drug_name as drugName,dr.drug_name as toDrugName,c.to_store_name as toStoreName,dr.quantity as num,dr.use_unit as unit,dr.refund_count,dr.discount_price as price,dr.create_time as orderTime,dr.status,ord.api_order_no as apiOrderNo from (\n" +
            "select a.cir_order_no,a.cir_info_no,a.cir_drug_id,b.from_drug_name,b.to_store_name,a.discount_price,a.use_unit,a.order_no  from (select * from his_prescription_info_drug where is_cirulation=1 and pharmacy_id=#{drugCirculation.storeId})a\n" +
            "left join (select * from his_drug_circulation where from_store_id=#{drugCirculation.storeId}) b on a.drug_id=b.from_drug_id\n" +
            "where  b.id is not  null\n" +
            "<if test=\"drugCirculation.drugName!=null and drugCirculation.drugName!=''\">\n" +
            "\t and (a.drug_name LIKE CONCAT('%',#{drugCirculation.drugName},'%'))\n" +
            "</if>\n" +
            ")c\n" +
            "left join his_prescription_info_drug dr on dr.info_no=c.cir_info_no and dr.drug_id = c.cir_drug_id " +
            "left join (select order_no,api_order_no from his_prescription_order) ord on ord.order_no=c.order_no " +
            "where 1=1 " +
            "<if test=\"drugCirculation.orderNo!=null and drugCirculation.orderNo!=''\">\n" +
            "\t and api_order_no=#{drugCirculation.orderNo}\n" +
            "</if>\n" +
            "</script>")
    List<DrugCirculationDetailRes> queryCirculationAll(@Param("drugCirculation")DrugCirculationToReq drugCirculationToReq);
}
